package com.sdut.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdut.material.mapper.RolePermissionMapper;
import com.sdut.material.pojo.Role;
import com.sdut.material.mapper.RoleMapper;
import com.sdut.material.pojo.RolePermission;
import com.sdut.material.pojo.vo.LayUISelectMVO;
import com.sdut.material.service.IPermissionService;
import com.sdut.material.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private IPermissionService permissionService;
    @Override
    public List<LayUISelectMVO> selectAllRoles() {
        List<Role> roles = roleMapper.selectList(new QueryWrapper<>());
        List<LayUISelectMVO> list = new ArrayList<>();
        for (Role role : roles) {
            LayUISelectMVO layUISelectMVO = new LayUISelectMVO();
            layUISelectMVO.setId(role.getId());
            layUISelectMVO.setName(role.getName());
            layUISelectMVO.setStatus(1);
            list.add(layUISelectMVO);
        }
        return list;
    }

    @Override
    public LayUITableResult selectByPage(Long page, Long limit) {
        IPage<Role> iPage = new Page<>(page,limit);
        IPage<Role> result = roleMapper.selectPage(iPage,new QueryWrapper<>());
        return LayUITableResult.ok(result.getTotal(),result.getRecords());
    }

    @Override
    public JSONResult insert(Role role, String permissions) {
        roleMapper.insert(role);
        String[] permissionIdArray = permissions.split(",");
        for (String permissionId : permissionIdArray) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(role.getId());
            rolePermission.setPermissionId(Integer.parseInt(permissionId));
            rolePermissionMapper.insert(rolePermission);
        }
        return JSONResult.ok("添加成功");
    }

    @Override
    public JSONResult selectRoleAndPermissions(Integer roleId) {
        Map<String,Object> map = new HashMap<>();
        Role role = roleMapper.selectById(roleId);
        map.put("role",role);
        List<LayUISelectMVO> layUISelectMVOList = permissionService.selectAllPermissions();
        map.put("allPermissions",layUISelectMVOList);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("permission_id");
        queryWrapper.eq("role_id",roleId);
        List list = rolePermissionMapper.selectObjs(queryWrapper);
        map.put("selectIds",list);
        return JSONResult.ok(map);
    }

    @Override
    public JSONResult updateRole(Role role, String permissions) {
        roleMapper.updateById(role);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",role.getId());
        rolePermissionMapper.delete(queryWrapper);
        String[] permissionArray = permissions.split(",");
        for (String permissionId : permissionArray) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(role.getId());
            rolePermission.setPermissionId(Integer.parseInt(permissionId));
            rolePermissionMapper.insert(rolePermission);
        }
        return JSONResult.ok("修改成功");
    }

    @Override
    public void deleteById(Integer roleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        rolePermissionMapper.delete(queryWrapper);
        roleMapper.deleteById(roleId);
    }
}
